package com.ggh.jinjilive.ui.mine.personal.dressup;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentDressUpBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.mine.personal.DressUpDataMobile;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mine.personal.dressup.adapter.DressUpAdapter;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.ggh.jinjilive.views.GridItemDecoration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DressUpFragment extends BaseFragment<FragmentDressUpBinding> implements AddItemOnClickListener {
    private DressUpAdapter dressUpAdapter;

    private int type;
    public DressUpFragment(int type){

        this.type = type;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_dress_up;
    }

    @Override
    public void initView() {
        super.initView();
        //适配器
        dressUpAdapter = new DressUpAdapter();
        dressUpAdapter.setAddItemOnClickListener(this);
        binding.rootRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        binding.rootRecyclerView.addItemDecoration(new GridItemDecoration(3, SizeUtils.dp2px(5),true));
        binding.rootRecyclerView.setAdapter(dressUpAdapter);

    }

    @Override
    public void initData() {
        super.initData();
        Map<String, Object> params = new HashMap<>();
        params.put("type",type);
        GeneralUtils.infoFrameList(params, new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                List<DressUpDataMobile> dressUpDataMobileList = (List<DressUpDataMobile>) mObject;
                if(null != dressUpDataMobileList && dressUpDataMobileList.size() > 0){
                    dressUpAdapter.setAdd(dressUpDataMobileList);
                }
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

    }

    @Override
    public void onSkipClicked(int position, Object mObject) {
        DressUpDataMobile dressUpDataMobile = (DressUpDataMobile) mObject;
        if(dressUpDataMobile.getIs_buy() == 1){
            Map<String, Object> params = new HashMap<>();
            params.put("type",type);
            params.put("frame_id",dressUpDataMobile.getId());
            GeneralUtils.infoSaveFrame(params, new AddOnClickListener1() {
                @Override
                public void onSkipClicked(Object mObject) {
                    String picture = (String) mObject;
                    ToastUtils.make().show(picture);
                    initData();
                }
            });
        }else {

        }
    }


}
